package com.hotel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.dto.RoomInventoryDTO;
import com.hotel.entity.RoomInventory;
import com.hotel.mapper.RoomInventoryMapper;
import com.hotel.service.RoomInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class RoomInventoryServiceImpl extends ServiceImpl<RoomInventoryMapper, RoomInventory> implements RoomInventoryService {

    @Autowired
    private RoomInventoryMapper roomInventoryMapper;

    /**
     * 根据日期和房型id查询库存
     * @param date
     * @param roomTypeId
     * @return
     */
    @Override
    public RoomInventory getByDateAndRoomTypeId(LocalDateTime date, Integer roomTypeId) {
        return roomInventoryMapper.getByDateAndRoomTypeId(date, roomTypeId);
    }

    @Override
    public void updateRoomInventory(RoomInventoryDTO roomInventoryDTO) {
        //操作类型
        Integer type = roomInventoryDTO.getType();
        Integer roomTypeId = roomInventoryDTO.getRoomTypeId();
        LocalDateTime beginTime = roomInventoryDTO.getBeginTime();
        LocalDateTime endTime = roomInventoryDTO.getEndTime();

        //0.新增一间房间
        if (type == 0) {
            addOneRoom(roomTypeId);
        }

        //1.预订或入住
        if(type == 1){
            bookOrCheckIn(roomTypeId, beginTime, endTime);
        }

        //2.取消预订或者退住
        if(type == 2){
            cancelBookOrCheckOut(roomTypeId, beginTime, endTime);
        }

        //3.增加房间类型
        if(type == 3){
            addRoomType(roomTypeId);
        }

        //4.删除房间类型
        if(type == 4){
            deleteRoomType(roomTypeId);
        }

        //5.删除一间房间
        if(type == 5){
            deleteOneRoom(roomTypeId);
        }

    }

    //删除一间房间
    private void deleteOneRoom(Integer roomTypeId) {
        roomInventoryMapper.deleteOneRoom(roomTypeId);
    }

    //删除房间类型
    private void deleteRoomType(Integer roomTypeId) {
        roomInventoryMapper.deleteRoomType(roomTypeId);
    }

    //增加房间类型
    private void addRoomType(Integer roomTypeId) {
        //获取当前时间,只精确到天
        LocalDateTime now = LocalDateTime.parse(LocalDateTime.now().format( DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //循环生成未来60天的库存
        for (int i = 0; i <= 60; i++) {
            roomInventoryMapper.addRoomType(roomTypeId, now.plusDays(i));
        }
    }

    private void cancelBookOrCheckOut(Integer roomTypeId, LocalDateTime beginTime, LocalDateTime endTime) {
        LocalDateTime beginDate = null;
        LocalDateTime endDate = null;
        if (beginTime.getHour() >= 14){
            beginDate = LocalDateTime.parse(beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        if(beginTime.getHour() < 14){
            beginDate = LocalDateTime.parse(beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).plusDays(-1);
        }
        if(endTime.getHour() <= 14){
            endDate = LocalDateTime.parse(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).plusDays(-1);
        }
        if(endTime.getHour() > 14){
            endDate = LocalDateTime.parse(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        roomInventoryMapper.cancelBookOrCheckOut(roomTypeId, beginDate, endDate);
    }

    private void bookOrCheckIn(Integer roomTypeId, LocalDateTime beginTime, LocalDateTime endTime) {
        LocalDateTime beginDate = null;
        LocalDateTime endDate = null;
        if (beginTime.getHour() >= 14){
            beginDate = LocalDateTime.parse(beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        if(beginTime.getHour() < 14){
            beginDate = LocalDateTime.parse(beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).plusDays(-1);
        }
        if(endTime.getHour() <= 14){
            endDate = LocalDateTime.parse(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).plusDays(-1);
        }
        if(endTime.getHour() > 14){
            endDate = LocalDateTime.parse(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        roomInventoryMapper.bookOrCheckIn(roomTypeId, beginDate, endDate);
    }

    //新增一间房间
    private void addOneRoom(Integer roomTypeId) {
        roomInventoryMapper.addOneRoom(roomTypeId);
    }
}
